<?php

return array(
    'service_manager' => array(
        'invokables' => array(
            'Zend\Permissions\Acl\Acl' => 'Zend\Permissions\Acl\Acl',
        ),
        'factories' => array(
            'Authorization\Service\AuthorizationService' => function ($sm) {
                $config = $sm->get('Config');
                $authorizationConfig = $config['authorization']; assert(is_array($authorizationConfig));
                $roleRepository = $sm->get('User\Repository\RoleRepository');
                $rolesTree = $roleRepository->getRolesTree(); // TODO: should AuthorizationService depend on RoleRepository directly?
                $authenticationService = $sm->get('Authentication\Service\AuthenticationService');
                
                $service = new \Authorization\Service\AuthorizationService();
                $service->setAcl($sm->get('Zend\Permissions\Acl\Acl'));
                $service->setRolesTree($rolesTree);
                $service->setPermissions($authorizationConfig);
                $service->setRole($authenticationService->getIdentity() ? $authenticationService->getIdentity()->getRole() : null);
                return $service;
            },
            'Authorization\Service\RouteListener' => function ($sm) {
                $service = new \Authorization\Service\RouteListener();
                $service->setAuthorizationService($sm->get('Authorization\Service\AuthorizationService'));
                return $service;
            },
        ),
    ),
    'router' => array(
        'routes' => array(
            'auth' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/auth[/:action]',
                    'defaults' => array(
                        'controller' => 'Authentication\Controller\Authentication',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'authorization' => __DIR__ . '/../view',
        ),
    ),        
);